import pathlib
from datetime import datetime

import qrcode

from forward.config import get_settings


class QRCodeUtils:
    @classmethod
    async def create_qr_code(cls, text: str, file_name: str):

        img = qrcode.make(text)
        fload = "qrcode"
        save_path = pathlib.Path(get_settings().static_directory, fload)
        save_path.mkdir(parents=True, exist_ok=True)
        # file_name = datetime.now().timestamp().__str__() + ".png"
        file_name = file_name + ".png"
        file_path = pathlib.Path(save_path, file_name)
        with open(file_path, "wb") as f:
            img.save(f)
            f.close()
        # 保存二维码链接
        return pathlib.Path(get_settings().static_prefix, fload, file_name).as_posix()
